package top.codetheory._01_动态数组.leetcode;

/**
 * @author:
 * @since: 2023/10/26 16:10
 **/
public class _35_搜索插入位置 {
    public static int searchInsert(int[] nums, int target) {
        // 定义边界, 左闭右闭
        int left = 0, right = nums.length - 1, middle = -1;
        while (left <= right) {
            middle = (left + right) >>> 1;
            if (target < nums[middle] ) {
                right = middle - 1;
            }else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }

        }
        return left;
    }

    public static void main(String[] args) {
        int[] nums = {1,3, 5, 6};
        int target = 0;
        System.out.println(searchInsert(nums, target));
    }
}